package daoJPA;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

import dao.IUserDAO;
import entityJPA.User;

public class JPAUserDAO implements IUserDAO {
	
    private EntityManagerFactory emf = null;

	public JPAUserDAO(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
	
	
	@Override
	public List<User> findAll() {
		EntityManager em = getEntityManager();
		Query q = null;
		try {
			CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
			cq.select(cq.from(User.class));
			q = em.createQuery(cq);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return q.getResultList();
	}

	@Override
	public User findById(int id) {
		EntityManager em = getEntityManager();
        try {
            return em.find(User.class, id);
        } finally {
            em.close();
        }
	}
	
	@SuppressWarnings("unchecked")
	public List<User> findByUserName(String userName){
		List<User> lista=new ArrayList<User>();
		try{
			EntityManager em=getEntityManager();
			em.getTransaction().begin();
			Query query = em.createQuery("SELECT u FROM User u where u.username = '" + userName + "'");
		    lista = (List<User>) query.getResultList();
			em.getTransaction().commit();
			return lista;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return lista;
	}

	private void generateUserName(User user){
		int contor=0;
		if(user.getLastName().length()<=5 && user.getUsername() == null){
			user.setUsername(user.getLastName() + user.getFirstName());
			user.setUsername(user.getUsername().substring(0,6));
		}
		else
			user.setUsername(user.getLastName().substring(0,5) + user.getFirstName().charAt(contor));
		contor++;
		while(findByUserName(user.getUsername()).size() != 0){
				user.setUsername(user.getUsername() + user.getFirstName().charAt(contor));
				contor++;
		}
		
		
	}
	
	@Override
	public void insert(User user) {
		EntityManager em = null;
		try {
		em = getEntityManager();
		em.getTransaction().begin();
		generateUserName(user);
		em.persist(user);
		
		em.getTransaction().commit();
		} finally {
            if (em != null) {
                em.close();
            }
        }
	}

	@Override
	public void delete(User user) {
		EntityManager em = null;
        try {
			em = getEntityManager();
			em.getTransaction().begin();
			User u = null;
	        try {
	            u = em.getReference(User.class, user.getIdUser());
	            u.getIdUser();
	        } catch (EntityNotFoundException ex){
	            ex.printStackTrace();
	        }
			em.remove(u);
			em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
	}

	@Override
	public void update(User user) {
		EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.merge(user);
            em.getTransaction().commit();
        } finally{
        	
        }
	}

}
